//
//  HomePageListPicView.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#define itemPadding 7.0
#define itemWidth 86
#define maxWidth (kScreenW - 40)
#define maxHeight maxWidth * (165.0f/337.0f)
#import "CapusDetailPictureView.h"

@interface CapusDetailPictureView ()
@property(nonatomic, strong) UIView *singalView; // 只有一张图
@property(nonatomic, strong) UIView *doubleView; // 2张 or 4张图
@property(nonatomic, strong) UIView *normalTypeView; // 其他
@property(nonatomic, strong) UIView *otherTypeView; // 三张
@property(nonatomic, strong) UIView *moreTypeView; // 5张
@property(nonatomic, strong) UIView *coverView;
@property(nonatomic, strong) UILabel*moreLb;
@end

@implementation CapusDetailPictureView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

                    
- (void)setUI{
    
    [self addSubview:self.singalView];

    [self.singalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [self addSubview:self.doubleView];
    [self.doubleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [self addSubview:self.normalTypeView];
    [self.normalTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [self addSubview:self.otherTypeView];
    [self.otherTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [self addSubview:self.moreTypeView];
    [self.moreTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
}



- (void)setIndex:(NSNumber *)index urls:(NSArray *)urls{
    if (index.intValue == 1) {
        self.singalView.hidden = NO;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = YES;
        self.moreTypeView.hidden = YES;
        [self.singalView.subviews makeObjectsPerformSelector:@selector(setImage:) withObject:nil];
        [self.singalView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj sd_setImageWithURL:[NSURL URLWithString:[urls objectAtIndex:idx]]];
        }];
    }
    
    if (index.intValue == 2) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = NO;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = YES;
        self.moreTypeView.hidden = YES;
        
        [self.doubleView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)obj sd_setImageWithURL:[NSURL URLWithString:urls[idx]]];
            }
        }];
    }
    if (index.intValue == 3) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = NO;
        self.otherTypeView.hidden = YES;
        self.moreTypeView.hidden = YES;
        [self.normalTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)obj sd_setImageWithURL:[NSURL URLWithString:urls[idx]]];
            }
        }];
    }
    if (index.intValue == 4) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = NO;
        self.moreTypeView.hidden = YES;
        [self.otherTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)obj sd_setImageWithURL:[NSURL URLWithString:urls[idx]]];
            }
        }];
    }
    if (index.intValue >= 5) {
        self.singalView.hidden = YES;
        self.doubleView.hidden = YES;
        self.normalTypeView.hidden = YES;
        self.otherTypeView.hidden = YES;
        self.moreTypeView.hidden = NO;
        [self.moreTypeView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)obj sd_setImageWithURL:[NSURL URLWithString:urls[idx]]];
            }
        }];
        
        self.coverView.hidden = index.intValue == 5;
        
        if (index.intValue > 5) {
            self.moreLb.text = [NSString stringWithFormat:@"+%d",index.intValue - 4];
        }else{
            self.moreLb.text = @"";
        }
    }
}

- (void)showImageDetail:(UITapGestureRecognizer *)ges{
    UIView *view = ges.view;
    if (self.done) {
        self.done(view.tag);
    }
}

- (UIView *)singalView{
    
    if (!_singalView) {
        _singalView = [[UIView alloc] init];
        _singalView.hidden = YES;
        
        UIImageView *img = [[UIImageView alloc] init];
        img.backgroundColor = RGBA(240, 240, 240, 1);
        img.userInteractionEnabled = YES;
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.layer.cornerRadius = 8.0f;
        img.layer.masksToBounds = YES;
        img.tag = 0;
        img.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
        [img addGestureRecognizer:tap];
        [_singalView addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.singalView);
            make.top.equalTo(self.singalView);
            make.right.bottom.equalTo(self.singalView);
        }];
    }
    return _singalView;
}

- (UIView *)doubleView{
    
    if (!_doubleView) {
        _doubleView = [[UIView alloc] init];
        _doubleView.hidden = YES;
        CGFloat picW = (maxWidth - itemPadding)/2;
        for (NSInteger i = 0; i < 2; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.userInteractionEnabled = YES;
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.cornerRadius = 8.0f;
            img.layer.masksToBounds = YES;
            [_doubleView addSubview:img];
            img.tag = i;
            img.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
            [img addGestureRecognizer:tap];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(picW);
                make.top.equalTo(self.doubleView);
                make.height.mas_equalTo(maxHeight);
                make.left.mas_equalTo(self.doubleView).offset(i*(picW + itemPadding));
            
            }];
        }
    }
    return _doubleView;
}

- (UIView *)normalTypeView{
    
    if (!_normalTypeView) {
        _normalTypeView = [[UIView alloc] init];
        _normalTypeView.hidden = NO;
        CGFloat picW = (maxWidth - itemPadding)/2;
        CGFloat picH = (maxHeight - itemPadding)/2;
        UIImageView *lastImg = nil;
        for (NSInteger i = 0; i < 3; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.userInteractionEnabled = YES;
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.cornerRadius = 8.0f;
            img.layer.masksToBounds = YES;
            img.tag = i;
            img.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
            [img addGestureRecognizer:tap];
            [_normalTypeView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0) {
                    make.top.left.bottom.equalTo(self.normalTypeView);
                    make.width.mas_equalTo(picW);
                }
                if (i == 1) {
                    make.top.equalTo(self.normalTypeView);
                    make.left.equalTo(self.normalTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(picW, picH));
                }
                if (i == 2) {
                    make.top.equalTo(self.normalTypeView).offset(picH + itemPadding);
                    make.left.equalTo(self.normalTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(picW, picH));
                }
            }];
            lastImg = img;
        }
    }
    return _normalTypeView;
}

- (UIView *)otherTypeView{
    if (!_otherTypeView) {
        _otherTypeView = [[UIView alloc] init];
        _otherTypeView.hidden = YES;
        CGFloat picW = (maxWidth - itemPadding)/2;
        CGFloat picH = (maxHeight - itemPadding)/2;
        CGFloat otherW = (picW - itemPadding)/2;
        CGFloat max = 4;
        for (int i = 0; i < max; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.userInteractionEnabled = YES;
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.cornerRadius = 8.0f;
            img.layer.masksToBounds = YES;
            img.tag = i;
            img.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
            [img addGestureRecognizer:tap];
            [_otherTypeView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0) {
                    make.top.left.bottom.equalTo(self.otherTypeView);
                    make.width.mas_equalTo(picW);
                }
                if (i == 1) {
                    make.top.equalTo(self.otherTypeView);
                    make.left.equalTo(self.otherTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(picW, picH));
                }
                if (i == 2) {
                    make.top.equalTo(self.otherTypeView).offset(picH + itemPadding);
                    make.left.equalTo(self.otherTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
                }
                if (i == 3) {
                    make.top.equalTo(self.otherTypeView).offset(picH + itemPadding);
                    make.left.equalTo(self.otherTypeView).offset(picW + otherW + 2 * itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
                }
            }];
        }
    }
    return _otherTypeView;
}
- (UIView *)moreTypeView{
    if (!_moreTypeView) {
        _moreTypeView = [[UIView alloc] init];
        _moreTypeView.hidden = YES;
        CGFloat picW = (maxWidth - itemPadding)/2;
        CGFloat picH = (maxHeight - itemPadding)/2;
        CGFloat otherW = (picW - itemPadding)/2;
        CGFloat max = 5;
        for (int i = 0; i < max; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.userInteractionEnabled = YES;
            img.backgroundColor = RGBA(240, 240, 240, 1);
            img.contentMode = UIViewContentModeScaleAspectFill;
            img.layer.cornerRadius = 8.0f;
            img.layer.masksToBounds = YES;
            img.tag = i;
            img.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
            [img addGestureRecognizer:tap];
            [_moreTypeView addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0) {
                    make.top.left.bottom.equalTo(self.moreTypeView);
                    make.width.mas_equalTo(picW);
                }
                if (i == 1) {
                    make.top.equalTo(self.moreTypeView);
                    make.left.equalTo(self.moreTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
                }
                if (i == 2) {
                    make.top.equalTo(self.moreTypeView);
                    make.left.equalTo(self.moreTypeView).offset(picW + otherW + 2 * itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
                }
                if (i == 3) {
                    make.top.equalTo(self.moreTypeView).offset(picH + itemPadding);
                    make.left.equalTo(self.moreTypeView).offset(picW + itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
                }
                if (i == 4) {
                    make.top.equalTo(self.moreTypeView).offset(picH + itemPadding);
                    make.left.equalTo(self.moreTypeView).offset(picW + otherW + 2 * itemPadding);
                    make.size.mas_equalTo(CGSizeMake(otherW, picH));
               
                    
                    _coverView = [[UIView alloc]init];
                    _coverView.backgroundColor = RGBA(0, 0, 0, 0.5);
                    _coverView.layer.cornerRadius = 8.0f;
                    _coverView.layer.masksToBounds = YES;
                    [_moreTypeView addSubview:_coverView];
                    
                    _moreLb = [[UILabel alloc]init];
                    _moreLb.textColor = [UIColor whiteColor];
                    _moreLb.font = font(17);
                    [_moreTypeView addSubview:_moreLb];
                    
                    [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.moreTypeView).offset(picH + itemPadding);
                        make.left.equalTo(self.moreTypeView).offset(picW + otherW + 2 * itemPadding);
                        make.size.mas_equalTo(CGSizeMake(otherW, picH));
                    }];
                    [_moreLb mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.center.equalTo(_coverView);
                    }];
                    
                    _coverView.tag = i;
                    _coverView.userInteractionEnabled = YES;
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageDetail:)];
                    [_coverView addGestureRecognizer:tap];
                }
            }];
        }
    }
    return _moreTypeView;
}

@end
